package com.umbrella.worker.web.controller.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.umbrella.worker.dto.AdminDO;
import com.umbrella.worker.dto.SupplierDO;
import com.umbrella.worker.query.AdminQuery;
import com.umbrella.worker.query.OrderQuery;
import com.umbrella.worker.query.RoleQuery;
import com.umbrella.worker.query.SupplierQuery;
import com.umbrella.worker.result.JsonResultDO;
import com.umbrella.worker.result.JsonResultSupport;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IAdminService;
import com.umbrella.worker.service.IOrderService;
import com.umbrella.worker.service.IRoleService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.MD5;
import com.umbrella.worker.util.PageBeanUtil;
import com.umbrella.worker.util.StringUtil;

@Controller
@RequestMapping(value = "/admin")
public class AdminContrlloer {

	@Autowired
	private IAdminService adminService;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private ISuppliersService suppliersService;
	
	@Autowired
	private IOrderService orderService;
	
	@RequestMapping(value = "/add.html", method = RequestMethod.GET)
	public ModelAndView add(ModelAndView mav, 
			HttpServletRequest request) {
		
		SupplierQuery query = new SupplierQuery();
		ResultDO result = suppliersService.list(query);
		List<SupplierDO> list = (List<SupplierDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY);
		
		mav.addObject("SUPPLIER_LIST", result.getModel(ResultSupport.FIRST_MODEL_KEY));
		
		RoleQuery rquery = new RoleQuery();
		result = roleService.list(rquery);
		mav.addObject("ROLE_LIST", result.getModel(ResultSupport.FIRST_MODEL_KEY));
		if(result.isSuccess()) {
			mav.setViewName("manager/admin/add");
		} else {
			mav.setViewName("error");
		}
		return mav; 
	}
	
	@RequestMapping(value = "/accountInfo.html", method = RequestMethod.GET)
	public ModelAndView accountInfo(ModelAndView mav,
			HttpServletRequest request) {
		
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if(supplierId == null) {
			return new ModelAndView("redirect:/manager/login2.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);		
		
		ResultDO result = orderService.list(query);
		if(result.isSuccess()) {
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			mav.addObject("count", count);
		}
		ResultDO suResult = suppliersService.get(supplierId);
		if(suResult.isSuccess()) {
			SupplierDO supplierDo = (SupplierDO) suResult.getModel(ResultSupport.FIRST_MODEL_KEY);
			mav.addObject("supplierInfo", supplierDo);
		}
		//ResultDO adResult = adminService.getBySupplierId(supplierId);
		Integer adminId = (Integer) request.getSession().getAttribute("MANAGER_ID");
		ResultDO adResult = adminService.getOne(adminId);
		if(suResult.isSuccess()) {
			AdminDO adminDo = (AdminDO) adResult.getModel(ResultSupport.FIRST_MODEL_KEY);
			mav.addObject("adminInfo", adminDo);
		}
		mav.setViewName("manager2/account/info");
		return mav;
	}
	
	@RequestMapping(value = "/agreementInfo.html", method = RequestMethod.GET)
	public ModelAndView agreementInfo(ModelAndView mav,
			HttpServletRequest request) {
		
		mav.setViewName("manager2/account/agreement");
		return mav;
	}
	
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public ModelAndView add(ModelAndView mav,
			AdminDO adminDO,
			HttpServletRequest request) {
		
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		adminDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		adminDO.setModifiAuthor(adminDO.getCreateAuthor());
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr("123456" 
				+ adminDO.getwAUsername());
		
		adminDO.setwAPassword(md5Pwd);
		
		ResultDO result = adminService.create(adminDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	@RequestMapping(value = "/remove/{id}.json", method = RequestMethod.GET)
	public ModelAndView remove(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		ResultDO result = adminService.remove(id);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("删除提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("删除提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	@RequestMapping(value = "/editPwd.html", method = RequestMethod.GET)
	public ModelAndView editPwd(ModelAndView mav, 
		
			HttpServletRequest request) {
		//mav.setViewName("manager/admin/editPwd");
		mav.setViewName("manager2/account/mPwd");
		return mav;
	}
	
	@RequestMapping(value = "/editPwd.html", method = RequestMethod.POST)
	public ModelAndView editPwd123(ModelAndView mav, AdminDO adminDO,
			HttpServletRequest request) {
		
		Integer adminId = (Integer) request.getSession().getAttribute("MANAGER_ID");
		ResultDO result1=adminService.get(adminId);
		
		adminDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		adminDO.setModifiAuthor(adminDO.getCreateAuthor());
		
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr(adminDO.getwAPassword() 
				+ adminDO.getwAUsername());
		String md5Pwd2 = md5.getMD5ofStr(adminDO.getwOldPassword()
				+ adminDO.getwAUsername());
		adminDO.setwAPassword(md5Pwd);
		adminDO.setwOldPassword(md5Pwd2);
		ResultDO result = adminService.edPwd(adminDO);
		if(result.isSuccess()) {
			mav.setViewName("manager2/login");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/aeditPwd.html", method = RequestMethod.GET)
	public ModelAndView aeditPwd(ModelAndView mav, 
		
			HttpServletRequest request) {
		//mav.setViewName("manager/admin/editPwd");
		mav.setViewName("manager/admin/mPwd");
		return mav;
	}
	
	@RequestMapping(value = "/aeditPwd.html", method = RequestMethod.POST)
	public ModelAndView aeditPwd(ModelAndView mav, AdminDO adminDO,
			HttpServletRequest request) {
		
		Integer adminId = (Integer) request.getSession().getAttribute("MANAGER_ID");
		ResultDO result1=adminService.get(adminId);
		
		adminDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		adminDO.setModifiAuthor(adminDO.getCreateAuthor());
		
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr(adminDO.getwAPassword() 
				+ adminDO.getwAUsername());
		String md5Pwd2 = md5.getMD5ofStr(adminDO.getwOldPassword()
				+ adminDO.getwAUsername());
		adminDO.setwAPassword(md5Pwd);
		adminDO.setwOldPassword(md5Pwd2);
		ResultDO result = adminService.edPwd(adminDO);
		if(result.isSuccess()) {
			return new ModelAndView("redirect:/manager/login2.html");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	
	
	@RequestMapping(value = "/stop/{id}.json", method = RequestMethod.GET)
	public ModelAndView stop(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		AdminDO adminDO = new AdminDO();
		
		adminDO.setId(id);
		adminDO.setStatus(0);
		ResultDO result = adminService.modifi(adminDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("停用提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("停用提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	@RequestMapping(value = "/start/{id}.json", method = RequestMethod.GET)
	public ModelAndView start(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		AdminDO adminDO = new AdminDO();
		
		adminDO.setId(id);
		adminDO.setStatus(1);
		ResultDO result = adminService.modifi(adminDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("启用提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("启用提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	@RequestMapping(value = "/detail/{id}.html", method = RequestMethod.GET)
	public ModelAndView detail(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
	
		SupplierQuery query = new SupplierQuery();
		ResultDO  result = suppliersService.list(query);
		if(result.isSuccess()) {
			mav.addObject("SUPPLIER_LIST", result.getModel(ResultSupport.FIRST_MODEL_KEY));			
		} else {
			mav.setViewName("error");
		}
		
		result = adminService.get(id);
		if(result.isSuccess()) {
			mav.addObject("MANAGER_INFO", (AdminDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager/admin/detail");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	/**
	 * 渠道商里重置密码
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editPass/{id}.json", method = RequestMethod.GET)
	public ModelAndView editPass(ModelAndView mav,
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		ResultDO resul=adminService.getOne(id);
		
		AdminDO adminDO =(AdminDO) resul.getModel(ResultSupport.FIRST_MODEL_KEY);
		if(adminDO!=null) {
			MD5 md5 = new MD5();
			String md5Pwd = md5.getMD5ofStr("123456" 
					+ adminDO.getwAUsername());
			
			adminDO.setwAPassword(md5Pwd);
			
			ResultDO result = adminService.modifi(adminDO);
			
			if(result.isSuccess()) {
				jsonResultDO.setInfo("提交成功");
				jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
				mav.addObject("JSON_DATA", jsonResultDO);
			} else {
				jsonResultDO.setInfo("提交失败");
				jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
				mav.addObject("JSON_DATA", jsonResultDO);
			}
		}else {
			jsonResultDO.setInfo("提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	/**
	 * 管理员列表
	 * @param mav
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView query(ModelAndView mav,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		AdminQuery query = new AdminQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = adminService.list(query);
		System.out.println(result);
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/admin/list");
		} else {
			mav.setViewName("error");
		}
		return mav;
		
	}
	
	/**
	 * 渠道商下的管理员列表
	 * @param mav
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/supplierlist/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView supplierlist(ModelAndView mav,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		//渠道商id
		Integer supplierId = (Integer)  request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		
		AdminQuery query = new AdminQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		query.setSupplierId(supplierId);
		
		ResultDO result = adminService.list(query);
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/admin/adminlist");
		} else {
			mav.setViewName("error");
		}
		return mav;
		
	}
	
	/**
	 * 重置密码
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editPassword/{id}.json", method = RequestMethod.GET)
	public ModelAndView editPassword(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		ResultDO resul=adminService.get(id);
		
		if(resul.isSuccess()){
			AdminDO adminDO =(AdminDO) resul.getModel(ResultSupport.FIRST_MODEL_KEY);
			
			//设置统一密码  123456
			MD5 md5 = new MD5();
			String md5Pwd = md5.getMD5ofStr("123456"
					+ adminDO.getwAUsername());
			
			adminDO.setwAPassword(md5Pwd);
			
			ResultDO result = adminService.modifi(adminDO);
			
			if(result.isSuccess()) {
				jsonResultDO.setInfo("重置密码提交成功");
				jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
				mav.addObject("JSON_DATA", jsonResultDO);
			} else {
				jsonResultDO.setInfo("重置密码提交失败");
				jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
				mav.addObject("JSON_DATA", jsonResultDO);
			}
		}else{
			jsonResultDO.setInfo("重置密码提交失败，没有权限！");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		
		return mav;
	}
	
	@RequestMapping(value = "/toPassword.html", method = RequestMethod.GET)
	public ModelAndView toPassword(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager2/admin/adminedit");
		return mav;
	}
	
	/**
	 * 修改密码
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updatePassword.html", method = RequestMethod.POST)
	public ModelAndView updatePassword(ModelAndView mav, 
			HttpServletRequest request) {
		
		//获取登录信息
		Integer ids=(Integer) request.getSession().getAttribute("MANAGER_ID");
		System.out.println(ids);
		if(ids!=null){
			ResultDO resul=adminService.get(ids);
			AdminDO adminDO =(AdminDO) resul.getModel(ResultSupport.FIRST_MODEL_KEY);
			System.out.println(adminDO);
			//旧密码判断
			String oldpass=request.getParameter("oldpassword");
			MD5 md5 = new MD5();
			String md5Pwd = md5.getMD5ofStr(oldpass
					+ adminDO.getwAUsername());
			
			adminDO.setwAPassword(md5Pwd);
			ResultDO result = adminService.validate(adminDO);
			if(result.isSuccess()) {
				//新密码
				String newpassword=request.getParameter("newpassword");
				String md5Pwd2 = md5.getMD5ofStr(newpassword
						+ adminDO.getwAUsername());
				adminDO.setwAPassword(md5Pwd2);
				
				ResultDO result2 = adminService.modifi(adminDO);
				
				if(result2.isSuccess()) {
					return new ModelAndView("redirect:/admin/list/1.html");
				} else {
					mav.setViewName("error");
				}
				
			}else{
				mav.setViewName("manager2/login");
			}
			
		}else{
			mav.setViewName("manager2/login");
		}
		
		return mav;
	}
	
	/**
	 * 渠道商里添加账号功能
	 * @param mav
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toaddAdmin.html", method = RequestMethod.GET)
	public ModelAndView toaddAdmin(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("manager2/admin/adminadd");
		return mav;
	}
	
	
	/**
	 * 渠道商里添加管理员
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addAdmin.html", method = RequestMethod.POST)
	public ModelAndView addAdmin(ModelAndView mav, AdminDO adminDO,
			HttpServletRequest request) {
		//统一密码
		MD5 md5 = new MD5();
		String md5Pwd = md5.getMD5ofStr("123456" + adminDO.getwAUsername());
		
		adminDO.setwAPassword(md5Pwd);
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if(supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		adminDO.setwASupplierId(supplierId);
		adminDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		adminDO.setModifiAuthor(adminDO.getCreateAuthor());

		ResultDO result = adminService.create(adminDO);
		if (result.isSuccess()) {
			return new ModelAndView("redirect:/admin/supplierlist/1.html");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	/**
	 * 批量删除
	 * @param mav
	 * @param idArray
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/removeall/{idArray}.json", method = RequestMethod.GET)
	public ModelAndView remove(ModelAndView mav, 
			@PathVariable(value="idArray") String idArray,
			HttpServletRequest request) {
		
		boolean isSuccess = true;
		JsonResultDO jsonResultDO = new JsonResultSupport();
		System.out.println(idArray);
		String[] ids = idArray.split("-");
		System.out.println(ids.length);
		ResultDO result = new ResultSupport();
		for(String id : ids) {
			System.out.println(id);
			result = adminService.remove(Integer.parseInt(id));
			if(!result.isSuccess()) {
				isSuccess = false;
				break;
			}
		}
		if(isSuccess) {
			jsonResultDO.setInfo("批量删除提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("批量删除提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
}
